﻿using System.ComponentModel;
using System.Linq;
using Ext.Net;

namespace KamLib.Controls
{
    public class Grid : GridPanel
    {
        public ListLogic Logic;

        //protected override void OnBeforeClientInit(Observable sender)
        //{
        //    base.OnBeforeClientInit(sender);
        protected override void OnPreRender(System.EventArgs e)
        {
            base.OnPreRender(e);

            if (HideHeaders) return;

            var cols = ColumnModel.Columns.OfType<IColumnFilterFactory>().ToArray();
            if (cols.Length == 0) return;

            var filter = Plugins.OfType<GridFilters>().FirstOrDefault();
            if (filter == null)
                Plugins.Add(filter = new GridFilters { UpdateBuffer = 1000 });

            foreach (var col in cols)
            {
                var f = col.NewFilter();
                if (f != null && filter.Filters.Where(a => a.DataIndex == f.DataIndex).FirstOrDefault() == null)
                    filter.Filters.Add(f);
            }
        }
    }

    public class CompactGrid : Grid
    {
        [DefaultValue(1)]
        public override int ClicksToEdit { get { return 1; } }

        [DefaultValue("kl-grid-flat-title")]
        public override string Cls { get { return "kl-grid-flat-title"; } }

        [DefaultValue(true)]
        public override bool HideHeaders
        {
            get { return ViewState.Try("HideHeaders", true); }
            set { base.HideHeaders = value; }
        }

    }

}
